import {
    View, Text, Image, Dimensions, TouchableOpacity, Alert,
    AsyncStorage, Platform, DeviceEventEmitter
} from 'react-native';
import React, {Component} from 'react';

import {Button, List, InputItem, Toast} from 'antd-mobile-rn'
import LocalImg from '../const/Images'
import Urls, {getRealUrl} from '../const/Urls';
import {NavigationActions} from "react-navigation";

const {width, height} = Dimensions.get('window');

class Setting extends Component {

    constructor(props) {
        super(props);
        this.state = {
            user: {
                token: '',
                pilotCode: '',
                pilotName: '',
                bmbm: '',
            },
        };
        this.setUser();
    }


    setUser() {
        AsyncStorage.getItem("user", (error, result) => {
            if (!error) {
                const data = JSON.parse(result);
                if (data) {
                    this.setState({
                        user: {
                            token: data['token'],
                            pilotCode: data['pilotCode'],
                            pilotName: data['pilotName'],
                            bmbm: data['bmbm']
                        }
                    });
                }

            }
        })
    }


    render() {
        return (
            <View style={{height: height, width: width, backgroundColor: '#ffffff'}}>
                <View style={{
                    flexDirection: 'row',
                    flex: 0,
                    height: 60,
                    alignItems: 'center',
                    justifyContent: 'center',
                    backgroundColor: '#0066FF'
                }}>
                    <View style={{flex: 1, padding: 10}}>
                        <TouchableOpacity onPress={() => this.props.navigation.pop()}>
                            <Image source={LocalImg['backImg']} style={{height: 30, width: 30, resizeMode: 'contain'}}/>
                        </TouchableOpacity>
                    </View>
                    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                        <Text style={{fontSize: 20, color: '#ffffff'}}>设置</Text>
                    </View>

                    <View style={{flex: 1, alignItems: 'flex-end', padding: 10}}>
                        <TouchableOpacity>
                        </TouchableOpacity>
                    </View>
                </View>

                {this.loginView()}


                <View style={{margin: 10}}>
                    <Text>当前版本:{Platform.Version}</Text>
                </View>
            </View>
        );
    }

    loginView() {
        if (this.state.user.token) {
            return (
                <View>
                    <View key='1' style={{margin: 10}}>
                        <Button type='warning' onClick={() => this.clearUser()}>退出登陆</Button>
                    </View>
                    <View key='2' style={{margin: 10}}>
                        <Button type='primary' onClick={() => this.changePwd()}>修改密码</Button>
                    </View>
                </View>)
        }
    }

    clearUser() {
        AsyncStorage.removeItem("user", (error, result) => {
            if (!error) {
                DeviceEventEmitter.emit('login');
                this.props.navigation.pop();
            }
        })
    }

    changePwd() {

    }


}

export default Setting;